# -*- coding:utf-8 -*-

"""
=============================
this is a feibot product,
all rights reserved.
www.feibot.com
@author: Gu Tianyuan
@email: g@feibot.com
@time: ${DATE}:${TIME}
@product: ${PRODUCT_NAME}
=============================
"""

import sys

from PyQt5.QtWidgets import QDialog

from PyQt5.QtGui import *

import gtyTools.gtyTypes
from uiFiles import ui_about
from gtyTools import tools, gitTools
from gtyConfig import language, systemConfig, configFileHandler
import uiTools


class About(QDialog, ui_about.Ui_AboutDialog):

    def __init__(self, mainPage, eventQ, parent=None):
        super(About, self).__init__(parent)
        self.setupUi(self)
        uiTools.centerAndSetIcon(self)
        # 处理不同的语言
        self.setWindowTitle("About")
        self.l = language.lanFromFile()
        self.backButton.setText(language.about_back)

        self.mainPage = mainPage
        self.eventQ = eventQ

        self.machine = configFileHandler.UserConfigHandler()
        userDefinedPicUrl = self.machine.read("machine", "aboutImage", "string", "")
        hardwareType = self.machine.read("machine","hardwareType").upper()
        if "/" in userDefinedPicUrl:
            try:
                self.aboutLabel.setPixmap(QPixmap(userDefinedPicUrl))
            except Exception as e:
                print(e)
        else:
            if self.l == 'english':
                self.aboutLabel.setPixmap(QPixmap("/home/feibot/platform/resources/pic2.png"))
            else:
                if hardwareType == "F800":
                    self.aboutLabel.setPixmap(QPixmap("/home/feibot/platform/resources/pic1.png"))
                elif hardwareType == "A400":
                    self.aboutLabel.setPixmap(QPixmap("/home/feibot/platform/resources/A400.jpg"))
                elif hardwareType == "F2000":
                    self.aboutLabel.setPixmap(QPixmap("/home/feibot/platform/resources/F2000.jpg"))
                elif hardwareType == "T400":
                    self.aboutLabel.setPixmap(QPixmap("/home/feibot/platform/resources/pic1.png"))

        # 配置的控制接口
        self.configHandlers = gtyTools.gtyTypes.ConfigHandlers()

        self.modeBtn.setText('------')
        self.releaseMode = self.configHandlers.state.read('machine', 'releaseMode')
        if 'release' in self.releaseMode:
            self.modeBtn.setText('normal mode')
        if 'debug' in self.releaseMode:
            self.modeBtn.setText('debug mode')
        self.modeBtn.clicked.connect(self.setReleaseMode)

        # 显示ip地址
        ips = tools.getIpAddr()
        if ips is not []:
            ipStr = ''
            for ip in ips:
                ipStr += ip + ','
            ipStr = ipStr[:-1]
            self.textBrowser.append('ip: ' + ipStr)
        self.textBrowser.append('software version: ' + systemConfig.softwareVersion)
        self.textBrowser.append('firmware version: ' + self.configHandlers.state.read('machine', 'firmwareversion'))
        # 显示Git版本状态信息
        commitId, branchName = gitTools.getGitHeadInfo()
        if commitId != '':
            self.textBrowser.append('commit id:' + commitId + ' ')
        if branchName != '':
            self.textBrowser.append('branch:' + branchName + " ")
        self.setGeometry(-200, -200, self.width(), self.height())

    # 发生事件
    def sendEvent(self, task, eventName, eventData=None):
        if eventData is None:
            eventData = []
        e = [eventName, eventData]
        try:
            if task.upper() in self.eventQ.keys():
                self.eventQ[task.upper()].put(e)
        except Exception as e:
            print(e)

    # 设置调试模式
    def setReleaseMode(self):
        print(__file__, 'setReleaseMode:', self.releaseMode)
        if self.releaseMode == 'release':
            self.sendEvent('UART', 'uart_sendReleaseMode', ['debug'])
            self.releaseMode = 'debug'
            self.configHandlers.state.write('machine', 'releaseMode', 'debug')
            self.modeBtn.setText('debug mode')
            self.sendEvent('UI', 'ui_updateMainDialogValue', {'releaseMode': 'debug'})
            return
        if self.releaseMode == 'debug':
            self.sendEvent('UART', 'uart_sendReleaseMode', ['release'])
            self.releaseMode = 'release'
            self.configHandlers.state.write('machine', 'releaseMode', 'release')
            self.modeBtn.setText('normal mode')
            self.sendEvent('UI', 'ui_updateMainDialogValue', {'releaseMode': 'release'})
            return
